﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClimateChangeChampionsData
{
    public partial class Pledge
    {
        #region static factory methods
        public static Pledge GetByEmailAddress(DataContext context, string emailAddress)
        {
            var tmp = from t in context.Pledges where t.EmailAddress == emailAddress select t;
            return tmp.SingleOrDefault();
        }

        public static Pledge GetByAuthenicationKey(DataContext context, string authenicationKey)
        {
            var tmp = from t in context.Pledges where t.AuthenticationKey.ToLower() == authenicationKey select t;
            return tmp.SingleOrDefault();
        }
        #endregion

        public string DisplayName
        {
            get
            {
                return string.Concat(FirstName, " ", LastName).Trim();
            }
        }

        /*private Acms.Bll.User user;
        public string SchoolName
        {
            get
            {
                if (user == null && UserId > 0)
                {
                    user = Acms.Bll.User.GetById(UserId);
                    return user.GetCustomProperty("School Name");
                }
                else
                {
                    return null;
                }
            }
        }*/

        /*public DateTime Created
        {
            get { return DateTime.SpecifyKind(_Created, DateTimeKind.Utc).ToLocalTime(); }
        }*/

        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (action == System.Data.Linq.ChangeAction.Insert)
            {
                char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
                Random rnd = new Random();
                _AuthenticationKey = string.Format("{0}{1}{2}{3}", chars[rnd.Next(0, chars.Length)], rnd.Next(0, 1000), chars[rnd.Next(0, chars.Length)], rnd.Next(0, 1000));
                if (_AuthenticationKey.Length > 10)
                    _AuthenticationKey = _AuthenticationKey.Substring(0, 10);
                @__Created = DateTime.UtcNow;
            }
        }

    }
}
